package com.winit.schedule.core.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * 日期工具类
 */
public class DateUtils {

    public static final String YYYY_MM_DD_HH_MM_SS = "yyyy-MM-dd HH:mm:ss";

    public static final String YYYYMMDDHHMMSS      = "yyyyMMddHHmmss";

    public static String format(Date date) {
        SimpleDateFormat sdf = new SimpleDateFormat(YYYY_MM_DD_HH_MM_SS);
        return sdf.format(date);
    }

    public static String format(Date date, String pattern) {
        SimpleDateFormat sdf = new SimpleDateFormat(pattern);
        return sdf.format(date);
    }

    public static long getNextValidTimeAfter4Cron(String cronExpression, Date date) throws ParseException {
        CronExpression cron = new CronExpression(cronExpression);
        Date nextDate = cron.getNextValidTimeAfter(date);
        if (null != nextDate) {
            return nextDate.getTime();
        }
        return 0;
    }
}
